﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace VK.Comm
{
    /// <summary>
    /// 字符串验证
    /// </summary>
    public class RegexStr
    {
       static string regText = "";

        #region //==== Email ====//
        /// <summary>
        /// 验证Email
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool CheckEmail(string str)
        {
            regText = @"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$";
            
                return Regex.IsMatch(str,regText);

        }
        #endregion

        #region //====  数字 ====//
        enum Num
        {
            Num = 1,//只能为数字
            Int = 2,//只能为正整数
            Float = 3//浮点数
        }
        /// <summary>
        /// 只能为数字
        /// </summary>
        /// <returns></returns>
        public static bool CheckNum(string str)
        {
            return CheckDigital(Num.Num,str);
        }
        /// <summary>
        /// 只能为正整数
        /// </summary>
        /// <returns></returns>
        public static bool CheckInt(string str)
        {
           return CheckDigital(Num.Int,str);
        }

        /// <summary>
        /// 浮点数
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool CheckFloat(string str)
        {
            return CheckDigital(Num.Float,str);//
        }

        
        private static bool CheckDigital(Enum en,string str)
        { 
            switch(Convert.ToInt32(en))
            {
                case 1:
                    regText = @"^[0-9]*[1-9][0-9]*$";
                    break;
                case 2:
                    regText = @"^-?\d+$";
                 break;
                case 3:
                    regText = @"^(-?\d+)(\.\d+)?$";
                    break;
            }
           return Regex.IsMatch(str, regText);

        }

        #endregion 

        #region //==== 字母 ====//

        enum letters
        {
            letter = 1,//由26个英文字母组成的字符串
            letter_Num = 2,//由数字、26个英文字母或者下划线组成的字符串
            letterNum = 3//由数字和26个英文字母组成的字符串
        }
        /// <summary>
        /// 由26个英文字母组成的字符串
        /// </summary>
        /// <returns></returns>
        public static bool CheckLetter(string str)
        {
            return CheckLetters(letters.letter, str);
        }

        /// <summary>
        /// 由数字、26个英文字母或者下划线组成的字符串
        /// </summary>
        /// <returns></returns>
        public static bool CheckLetter_Num(string str)
        {
            return CheckLetters(letters.letter_Num, str);
        }


        /// <summary>
        /// 由数字和26个英文字母组成的字符串 
        /// </summary>
        /// <returns></returns>
        public static bool CheckLetterNum(string str)
        {
            return CheckLetters(letters.letterNum,str);
        }

        private static bool CheckLetters(Enum letters, string str)
        {
            switch (Convert.ToInt32(letters))
            { 
                case 1:
                    regText = @"^[A-Za-z]+$";
                    break;
                case 2:
                    regText = @"^\w+$";
                    break;
                case 3:
                    regText = @"^[A-Za-z0-9]+$";
                    break;
            }

            return Regex.IsMatch(str, regText);
        }


        #endregion


        /// <summary>
        /// 密码是否相等
        /// </summary>
        /// <param name="pwd"></param>
        /// <param name="confirmPwd"></param>
        /// <returns></returns>
        public static bool IsEquals(string str1, string str2)
        {
            return str1.Equals(str2);
            
        }
        
    }

}
